const app = getApp()
Page({
  
  data: {
    ////////fabiao
    icontype: ["info_circle", "info"],


    //////////
    baseurl: app.globalData.baseUrl,
    
    userInfo: false,
    imgUrls: [
      '../../images/swiper1.png',
      '../../images/swiper2.png',
      '../../images/swiper3.png'
    ],
    indicatorDots: true,
    vertical: false,
    autoplay: true,
    interval: 3000,
    duration: 1200,

    msgList: [
      { url: "url", text: "欢迎来到咔咔维修平台~~~" },
      { url: "url", text: "快来下单体验一下吧~~~" },
      { url: "url", text: "还可以接单赚点钱哟~~~" }]
    
  },
  /////////收藏
  onCollectionTap: function (e){
    var that = this;
    var talkId = e.currentTarget.dataset.talkid;

    wx.request({
      url: app.globalData.baseUrl + '/shoucang/getShouCang',
      method: 'GET',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        "talkId": talkId,
        "buyerOpenid": wx.getStorageSync("openid")
      },//传入的参数
      success: function (res) {
        
        var shouCang = res.data.shouCang;
        // console.log("shouCang", shouCang);
        if (shouCang == false) {
          wx.request({
            url: app.globalData.baseUrl + '/shoucang/insertShouCang',
            data: {//获取数据传到后台
              "buyerOpenid": wx.getStorageSync("openid"),
              "talkId": talkId,

            },
            method: "POST",
            header: {
              'content-type': 'application/x-www-form-urlencoded'
            },
            success: function (res) {
              var result = res.data.success
              var toastText = "收藏成功！";
              if (result != true) {
                toastText = "收藏失败！" ;
              }
              wx.showToast({
                title: toastText,
                icon: '',
                duration: 2000
              });

              that.onShow()

            }

          })
        } else {
          wx.request({
            url: app.globalData.baseUrl + '/shoucang/deleteShouCang',
            data: {//获取数据传到后台
              "buyerOpenid": wx.getStorageSync("openid"),
              "talkId": talkId,

            },
            method: "GET",
            header: {
              'content-type': 'application/x-www-form-urlencoded'
            },
            success: function (res) {
              var result = res.data.success
              var toastText = "取消收藏成功！";
              if (result != true) {
                toastText = "取消收藏失败！";
              }
              wx.showToast({
                title: toastText,
                icon: '',
                duration: 2000
              });

              that.onShow()

            }

          })


        }
        
      }
    })

    
  },




/////////////
//////////////查看详情
  talkDetail:function(e){
    var talkId = e.currentTarget.dataset.talkid;
    wx.navigateTo({
      url: '../talkDetail/talkDetail?talkid=' + talkId 
    })
  },



  //////////////////发表说说
  addTalk:function(){
    wx.navigateTo({
      url: '../z/z'
   })
  },

  /////////////////////

/////////////说说列表
  talk: function () {    
    var that = this;
    var list=[];
    wx.request({
      url: app.globalData.baseUrl + '/talk/allTalkList',
      method: 'GET',
      data: {},//传入的参数
      success: function (res) {
        list = res.data.talkList;
        console.log("talkList:", list);
        if (list == null) {
          var toastText = '获取数据失败';
          wx.showToast({
            title: toastText,
            icon: '',
            duration: 2000
          });
        } else {
          var index=0;
          list.forEach((r) => {  //array是后台返回的数据
            index=index+1;
            wx.request({
              url: app.globalData.baseUrl + '/shoucang/getShouCang',
              method: 'GET',
              header: {
                'content-type': 'application/x-www-form-urlencoded'
              },
              data: {
                "talkId": r.talkId,
                "buyerOpenid": wx.getStorageSync("openid")
              },//传入的参数
              success: function (res) {
                //console.log("list[index].talkId", list[index].talkId);
                //console.log("collected", list[index].collected);
                var shouCang = res.data.shouCang;
                // console.log("shouCang", shouCang);
                if (shouCang == false) {
                  r.collected = false;
                  //list[index].collected = false;
                  //console.log(list[index].collected, list[index].collected);
                } else {
                  r.collected = true;
                  console.log(r.talkId, r.collected);
                  console.log(r.talkId, r);


                }
                if (index == list.length) {
                  that.setData({
                    talkList: list
                  });
                  console.log("list；", list);
                }
              }
            })
           
          })
          /*that.setData({
            talkList: list
          });
          console.log("list；", list);*/
         /* for (var index in list) {
            list[index].collected = false;
            //console.log("talkId", list[index].talkId);
            //console.log("index", index);
            
            wx.request({
              url: app.globalData.baseUrl + '/shoucang/getShouCang',
              method: 'GET',
              header: {
                'content-type': 'application/x-www-form-urlencoded'
              },
              data: {
                "talkId": list[index].talkId,
                "buyerOpenid": wx.getStorageSync("openid")
              },//传入的参数
              success: function (res) {
                //console.log("list[index].talkId", list[index].talkId);
                //console.log("collected", list[index].collected);
                var shouCang = res.data.shouCang;
               // console.log("shouCang", shouCang);
                if ( shouCang == false) {
                  
                  //list[index].collected = false;
                  //console.log(list[index].collected, list[index].collected);
                } else {
                  list[index].collected = true;
                  console.log(index, list[index].collected);
                  console.log(index, list[index]);
                  
                  
                }
              }
            })
            if(index==list.length-1){
              that.setData({
                talkList: list
              });
              console.log('talkList00:', list);
            }

          }*/
          
          


          /*that.setData({
            talkList: list
          });
          console.log('talkList00:' + list);*/
        

        }
      }
    })
    
    /*var that = this;
    wx.request({
      url: app.globalData.baseUrl + '/talk/allTalkList',
      method: 'GET',
      data: {},//传入的参数
      success: function (res) {
        var list = res.data.talkList;
        if (list == null) {
          var toastText = '获取数据失败';
          wx.showToast({
            title: toastText,
            icon: '',
            duration: 2000
          });
        } else {
          that.setData({
            talkList: list
          });
          console.log('talkList:' + list);
        }
      }
    })*/

  },

  
  

  
  /**
   * 生命周期函数——监听页面加载
   */
  onLoad:function(){
    //获取登录信息
    /*let userInfo = wx.getStorageSync("userInfo");
    if (null != userInfo && "" != userInfo) {
      this.setData({ userInfo: userInfo });
    }*/
  

   /*先把页面的的this保存到that,再用that，
    否则本方法下面wx.request之后直接用this的话，此this就不再是指页面的this，会出错*/
    //方法一：
    var that = this;
    wx.request({
      url: app.globalData.baseUrl+'/category1/listcategory1',
      method: 'GET',
      data: {},//传入的参数
      success: function (res) {
        var list = res.data.category1List;
        if (list == null) {
          var toastText = '获取数据失败' + res.data.errMsg;
          wx.showToast({
            title: toastText,
            icon: '',
            duration: 2000
          });
        } else {
          that.setData({
            category1Icon: list
          });
        }
      }
    })
    that.gongGao();
    that.talk();
    
    
    //方法二：
    /*先把页面的的this保存到that,再用that，
    否则本方法下面wx.request之后直接用this的话，此this就不再是指页面的this，会出错*/
    /*var that = this;
    wx.request({
      url: 'http://localhost:8080/demo/category1/listcategory1',
      method: 'GET',
      //data: {},//传入的参数
      header:{
        'content-type':'application/json'
      },
      success: function (res) {
        console.log(res);
        var list = res.data.category1List;
        that.setData({
          category1Icon: list
        });
      },
        fail:function (res){
          console.log("------fail------");
        }      
    })*/
  },


  catchTapCategory: function (e) {
    var that = this;
    var category1Id = e.currentTarget.dataset.category1id;
    console.log('category1Id:' + category1Id);
    
    wx.navigateTo({ url: '../tocategory/tocategory?category1id=' + category1Id 
    
    
    })
    
    
  },
  //////授权
  login: function (e) {
    var that = this
    console.log(e);
    if (e.detail.errMsg.indexOf("ok") != -1) {
      //this.setData({ userInfo: e.detail.userInfo });
      //wx.setStorageSync("userInfo", e.detail.userInfo);//将对象存入本地缓存
      wx.setStorageSync("city", e.detail.userInfo.city);//将地址存入本地缓存
      console.log("这是.....：" + e.detail.userInfo.nickName);
      wx.login({
        success: function (res) {
          console.log("res...code..：" + res.code);
          //var code=res.code;
          wx.request({
            url: app.globalData.baseUrl + '/userinfo/login',
            method: "POST",
            header: {
              'content-type': 'application/x-www-form-urlencoded'
            },
            data: {//获取数据传到后台
              code: res.code,
              avatarUrl: e.detail.userInfo.avatarUrl,
              userName: e.detail.userInfo.nickName,
              userGender: e.detail.userInfo.gender,
              userCity: e.detail.userInfo.city,
              userProvince: e.detail.userInfo.province,
              userCountry: e.detail.userInfo.country,
            },

            success: function (res) {
              console.log("这是....：" + res.data.userInfo.buyerOpenid);
              const user = res.data.userInfo
              ///
              if (user == null) {
                var toastText = '获取数据失败' + res.data.errMsg;
                wx.showToast({
                  title: toastText,
                  icon: '',
                  duration: 2000
                });
              } else {
                // 将返回的数据保存到全局的缓冲中，方便其他页面使用
                wx.setStorageSync("openid", user.buyerOpenid);//将对象存入本地缓存
                wx.setStorageSync("userInfo", user);//将对象存入本地缓存
                that.setData({ userInfo: user });

              }

              // 将返回的数据保存到全局的缓冲中，方便其他页面使用
              // wx.setStorageSync("openid", openid);//将对象存入本地缓存
            }
          })
        }
      })

    }
  },
 
  
  

  /**
   * 生命周期函数——监听页面显示
   */
  onShow:function(){
    /*先把页面的的this保存到that,再用that，
    否则本方法下面wx.request之后直接用this的话，此this就不再是指页面的this，会出错*/
   var that = this;
    wx.request({
      url: app.globalData.baseUrl +'/category1/listcategory1',
      method:'GET',
      data:{},//传入的参数
      success:function(res){
        var list = res.data.category1List;
        if(list == null){
          var toastText = '获取数据失败' + res.data.errMsg;
          wx.showToast({
            title:toastText,
            icon:'',
            duration:2000
          });
        }else{
          that.setData({
            category1Icon:list
          });
        }
      }
    })
    that.gongGao();
    that.talk();
  },

  showInput:function(){
    wx.navigateTo({
      url: '../homepage/search/search'
    })
  },

gongGao:function(){
  var that = this;
  wx.request({
    url: app.globalData.baseUrl + '/message/listmessage',
    method: 'GET',
    data: {},//传入的参数
    success: function (res) {
      var list = res.data.messageList;
      if (list == null) {
        var toastText = '获取数据失败';
        wx.showToast({
          title: toastText,
          icon: '',
          duration: 2000
        });
      } else {
        that.setData({
          msgList: list
        });
      }
    }
  })
},


  /*******下拉刷新********* */
  // 页面下拉刷新

  // 下拉刷新
  onPullDownRefresh: function () {
    // 显示顶部刷新图标
    wx.showNavigationBarLoading();
    var that = this;
    that.setData({
      category1Icon: null,
      talkList: null
    });
    setTimeout(function () {
    that.onShow()
    // 隐藏导航栏加载框
    wx.hideNavigationBarLoading();
    // 停止下拉动作
    wx.stopPullDownRefresh();
    }, 1500);
    
  }

  


  
  
  
  
})
